package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.LeaseAgreementService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Autowired
    private LeaseAgreementService leaseAgreementService;
    @Autowired
    private BrowsingHistoryService browsingHistoryServicel;


    /**
     * @author gengjian
     * @date 2024/12/12 23:21:53
     * 分页查询房间vo
     */
    @Override
    public void pageRoomItemVoByQuery(IPage<RoomItemVo> iPage, RoomQueryVo queryVo) {
        roomInfoMapper.pageRoomItemVoByQuery(iPage,queryVo);
    }


    /**
     * @author gengjian
     * @date 2024/12/15 16:29:20
     * 根据公寓id分页查询房间列表vo
     */
    @Override
    public void pageItemByApartmentId(IPage<RoomItemVo> page, Long id) {
        roomInfoMapper.pageItemByApartmentId(page,id);
    }

    /**
     * @author gengjian
     * @date 2024/12/15 17:58:30
     * 根据id获取房间的详细信息
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //根据房间id获取房间基本信息
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        //根据房间信息获取所属公寓id，再通过公寓id获取公寓信息
        ApartmentItemVo apartmentItemVo = apartmentInfoService.getApartmentItemVo(roomInfo.getApartmentId());
        //根据房间id获取图片列表
        List<GraphVo> graphVoList = graphInfoMapper.selectListGraphVoByItemTypeAndId(ItemType.ROOM,id);
        //根据房间id获取属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListAttrValueVoByRoomId(id);
        //根据房间id获取配套信息列表
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListFacilityInfoByRoomId(id);
        //根据房间id获取标签信息列表
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListLabelInfoByRoomId(id);
        //根据房间id获取支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListPaymentTypeByRoomId(id);
        //根据公寓id获取杂费列表
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListFeeValueVoByRoomId(roomInfo.getApartmentId());
        //根据房间id获取租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListLeaseTermByRoomId(id);
        //查询房间是否被删除
        int isDeleted = roomInfo.getIsDeleted();
        //创建房间详情vo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //封装
        roomDetailVo.setIsDelete(isDeleted == 1);
        roomDetailVo.setApartmentItemVo(apartmentItemVo);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setFeeValueVoList(feeValueVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        //根据房间id查询房间是否已入住
        LambdaQueryWrapper<LeaseAgreement> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LeaseAgreement::getRoomId,id)
                        .in(LeaseAgreement::getStatus,LeaseStatus.RENEWING,LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        long count = leaseAgreementService.count(queryWrapper);
        if (count > 0){
            //已经入住
            roomDetailVo.setIsCheckIn(true);
        }else {
            roomDetailVo.setIsCheckIn(false);
        }
        //保存浏览记录
        browsingHistoryServicel.saveHistory(LoginUserContext.getLoginUser().getUserId(), roomInfo.getId());
        //返回vo
        return roomDetailVo;
    }
}




